// Get element by id
function gId(id) {
    return document.getElementById(id);
}

// General controls
var tempDisplay = gId('temperature');
var systemModeDisplay = gId('systemmode');
var nightModeDisplay = gId('nightmode');
var tempUnitsDisplay = gId('tempunits');


// Temperature control
gId('btnTempUp').onclick = function () {
    if (temperature < 150) {
        temperature=temperature+5;
        showControls();
        updateTempreature();
    }

};

gId('btnTempDown').onclick = function () {
    if (temperature > 50) {
        temperature=temperature-5;
        showControls();
        updateTempreature();
    }
};


gId('btnTempDown').onmousedown  = function () { if(systemMode)gId('btnTempDown').className = 'down set'; };
gId('btnTempDown').onmouseup  = function () { if(systemMode)gId('btnTempDown').className = 'down'; };

// System mode control
gId('btnModeHome').onclick = function () { systemMode = 1;  showControls(); updateSystemMode(); };
gId('btnModeAway').onclick = function () { systemMode = 0;  showControls(); updateSystemMode(); };

// Night mode control
gId('btnNightOn').onclick = function () { nightMode = 1; showControls(); updateNightMode(); };
gId('btnNightOff').onclick = function () { nightMode = 0; showControls(); updateNightMode(); };

// Display functions
//
//----------------Temperature-----------------

function showControls(){



    //if (!connection){
    //}
    if (!mode){
		tempDisplay.innerHTML = "MAN";
		
		tempUnitsDisplay.className = 'off';
        
        gId('btnTempDown').className = 'down set';
        gId('btnTempUp').className = 'up set';
        
        nightModeDisplay.className = 'off';
        gId('btnNightOff').className = 'off set';
        gId('btnNightOn').className = 'on set';
        
        systemModeDisplay.className = 'off';
        gId('btnModeHome').className = 'home set';
        gId('btnModeAway').className = 'away set';
        
        return;
	}
	
    if (!systemMode){
    
        tempDisplay.innerHTML = "VAC";       
        tempUnitsDisplay.className = 'off';
        
        gId('btnTempDown').className = 'down set';
        gId('btnTempUp').className = 'up set';
        
        nightModeDisplay.className = 'off';
        gId('btnNightOff').className = 'off set';
        gId('btnNightOn').className = 'on set';
        
        systemModeDisplay.className = 'off';
        gId('btnModeHome').className = 'home';
        gId('btnModeAway').className = 'away set';
        
        return;
    }
    
    //alert("in show controls: connection: "+connection+" system mode: " + systemMode + " nightMode: " + nightMode);
    gId('btnTempDown').className = 'down';
    gId('btnTempUp').className = 'up';
    
    tempUnitsDisplay.className = '';    
    systemModeDisplay.className = '';
    gId('btnModeHome').className = 'home set';
    gId('btnModeAway').className = 'away';
        
    tempDisplay.innerHTML = temperature;
    //tempDisplay.innerHTML = "--";
	
    if (nightMode == 0) {
        nightModeDisplay.className = 'off';
        gId('btnNightOff').className = 'off set';
        gId('btnNightOn').className = 'on';
    }
    else {
        nightModeDisplay.className = '';
        gId('btnNightOn').className = 'on set';
        gId('btnNightOff').className = 'off';
    }
    
}

function updateNightMode() {
    ajaxRequest("/state.xml?NSMState="+nightMode);
 }
function updateSystemMode() { 
    ajaxRequest("/state.xml?HAState="+systemMode);
    }
function updateTempreature() {
        ajaxRequest("/state.xml?TEMPSetting="+temperature);
 }

//----------------AJAX-----------------
var ax;

function fV(tag) {
    var opentag = "<" + tag + ">";
    var closetag = "<\/" + tag + ">";
    index = ax.responseText.indexOf(opentag);
    startindex = index + opentag.length;
    endindex = ax.responseText.indexOf(closetag);
    return ax.responseText.slice(startindex, endindex);
}  


function ajaxRequest(filename) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        ax = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ax = new XMLHttpRequest();
    }
    if (ax) {
        
			ax.onreadystatechange = ajaxResponse;
		
        try {
            ax.open("GET", "http://"+address+"/"+filename, true);
            ax.send(null);
        } catch (err) {

        }
    }
	connection = 0;
}

function refresh(){
    ajaxRequest("state.xml");
}

function resetRefresh() {


}

function ajaxResponse() {
    
    switch (ax.readyState) {
        case 4:
    
            if (ax.status == 200){
                nightMode= parseInt(fV("NSMState"));
                systemMode= parseInt(fV("HAState"));
                temperature = parseInt(fV("TEMPSetting"));
				mode = parseInt(fV("Mode"));
             
                connection = 1;
            }else {
                connection = 0;
            }
            
            break;
        default:
            break;
    }
    showControls();
    
}
     
function myTimer()
{
var m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var d=new Date();

document.getElementById("currenttime").innerHTML=d.getHours()+":"+d.getMinutes();
document.getElementById("currentdate").innerHTML=m[d.getMonth()]+" - "+d.getDate();

}


// Initial values
var temperature = "--";
var systemMode = 1;
var nightMode = 1;
var connection = 0;
var mode = 1;  //Auto

// Init
refresh();
setInterval("refresh()", 5000);
showControls();
setInterval(function(){myTimer()},1000);
var parm = window.location.search;
var address = parm.substr(parm.indexOf("=")+1);






